package com.uyayo.cms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.uyayo.cms.entity.CmsLink;
import com.uyayo.cms.mapper.CmsLinkMapper;
import com.uyayo.cms.model.CmsLinkCondition;
import com.uyayo.cms.service.CmsLinkService;
import com.uyayo.framework.pagehelper.pojo.OrderBy;
import com.uyayo.framework.pagehelper.pojo.PageBean;
import com.uyayo.framework.pagehelper.util.OrderByUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


@Service("cmsLinkService")
public class CmsLinkServiceImpl implements CmsLinkService {

	private final CmsLinkMapper cmsLinkMapper;

	@Autowired
	public CmsLinkServiceImpl(CmsLinkMapper cmsLinkMapper) {
		this.cmsLinkMapper = cmsLinkMapper;
	}

	@Override
	public CmsLink getLinkById(Long id){
		return cmsLinkMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<CmsLink> selectLink(CmsLinkCondition cmsLinkCondition) {
		Example example = new Example(CmsLink.class);
		example.createCriteria();
		return cmsLinkMapper.selectByExample(example);
	}
	
	@Override
	public PageBean<CmsLink> selectLinkByPaging(Integer pageCurrent, Integer pageSize, String sort, String order, CmsLinkCondition cmsLinkCondition) {

		List<CmsLink> userList;
		//分页处理
		List<OrderBy> orderBies = new ArrayList<>();
		orderBies.add(new OrderBy("createTime", "create_time"));
		orderBies.add(new OrderBy("updateTime", "update_time"));

		OrderByUtil  orderByUtil = new OrderByUtil();
		String orderBy = orderByUtil.getOrderBy(orderBies, sort, order);
		PageHelper.startPage(pageCurrent, pageSize);
		
		Example example = new Example(CmsLink.class);
		if(!StringUtils.isEmpty(orderBy)){
			example.setOrderByClause(orderBy);
		}
		Criteria criteria = example.createCriteria();
		if(!StringUtils.isEmpty(cmsLinkCondition.getSiteName())){
			criteria.andLike("siteName","%"+cmsLinkCondition.getSiteName()+"%");
		}
		if(cmsLinkCondition.getBeginCreateTime()!=null || cmsLinkCondition.getEndCreateTime()!=null){
			criteria.andBetween("createTime", cmsLinkCondition.getBeginCreateTime(), cmsLinkCondition.getEndCreateTime());
		}
		if(Objects.nonNull(cmsLinkCondition.getStatus())){
			criteria.andEqualTo("status",cmsLinkCondition.getStatus());
		}
		userList = cmsLinkMapper.selectByExample(example);

		//取记录总条数
		PageInfo<CmsLink> pageInfo = new PageInfo<>(userList);
		
		return new PageBean<>(pageInfo.getTotal(), pageCurrent, pageSize, userList);
	}
	

	@Override
	public int createCmsLink(CmsLink cmsLink) {
		return cmsLinkMapper.insertSelective(cmsLink);
	}

	@Override
	public CmsLink selectOne(CmsLink cmsLink) {
		return cmsLinkMapper.selectOne(cmsLink);
	}

	@Override
	public int updateLink(CmsLink cmsLink) {
		return cmsLinkMapper.updateByPrimaryKeySelective(cmsLink);
	}

	@Override
	public int deleteLink(Integer id) {
		return cmsLinkMapper.deleteByPrimaryKey(id);
	}
	
}
